package com.blue.controller.author;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.blue.common.annotation.NotNull;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.constant.CommonConstant;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.author.RoleDTO;
import com.blue.po.author.RolePO;
import com.blue.service.author.IRoleService;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 角色表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/author/role")
@Tag(name = "角色表")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("roleName")
    public ResponseResult<?> list(RoleDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        RolePO RolePO = ConvertUtils.convert(RolePO.class, requestDTO);
        MPJQueryWrapper<RolePO> queryWrapper = QueryWrapperUtils.initQueryWrapper(RolePO, "t.sort", CommonConstant.ASC);
        Page<RolePO> page = new Page<>(pageNum, pageSize);
        Page<RolePO> pageResult = roleService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody RoleDTO requestDTO) {
        RolePO RolePO = ConvertUtils.convert(RolePO.class, requestDTO);
        roleService.save(RolePO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody RoleDTO requestDTO) {
        RolePO RolePO = ConvertUtils.convert(RolePO.class, requestDTO);
        roleService.updateById(RolePO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        roleService.delete(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody RoleDTO requestDTO) {
        RolePO RolePO = roleService.getById(requestDTO.getRoleId());
        return ResponseResult.okResult(RolePO);
    }

    @PostMapping("/relevanceUserInfo")
    @Operation(summary = "关联用户信息")
    @NotNull(value = "roleId", message = "角色ID不能为空！")
    public ResponseResult<?> relevanceUserInfo(@RequestBody RoleDTO requestDTO) {
        Map<String, Object> resultMap = roleService.relevanceUserInfo(requestDTO.getRoleId());
        return ResponseResult.okResult(resultMap);
    }

    @PostMapping("/relevanceUser")
    @Operation(summary = "关联用户")
    public ResponseResult<?> relevanceUser(@RequestBody Map<String, List<String>> map) {
        roleService.relevanceUser(map);
        return ResponseResult.okResult();
    }

    @PostMapping("/authorMenu")
    @Operation(summary = "菜单授权")
    public ResponseResult<?> authorMenu(@RequestBody Map<String, List<String>> map) {
        roleService.authorMenu(map);
        return ResponseResult.okResult();
    }
}
